<template>
  <div class="forget-container">
    <el-form ref="forgetForm" :model="forgetForm" :rules="forgetRules" class="login-form" auto-complete="off" label-position="left">
      <h3 class="title">重置密码</h3>
      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="mobile" />
        </span>
        <el-input v-model="forgetForm.mobile" name="username" type="text" auto-complete="off" placeholder="手机号">
          <el-button slot="append" :disabled="!sendVerifyCode" @click="handleVerify('forgetForm', 'mobile', forgetForm.mobile)">{{ btnmsg }}</el-button>
        </el-input>
      </el-form-item>
      <el-form-item prop="verifyCode">
        <span class="svg-container">
          <svg-icon icon-class="verifycode" />
        </span>
        <el-input v-model="forgetForm.verifyCode" name="verifyCode" type="text" auto-complete="off" placeholder="验证码" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input v-model="forgetForm.password" name="password" type="password" auto-complete="off" placeholder="新密码" />
      </el-form-item>
      <el-form-item prop="confirmPass">
        <span class="svg-container">
          <svg-icon icon-class="confirmpass" />
        </span>
        <el-input v-model="forgetForm.confirmPass" name="confirmPass" type="password" auto-complete="off" placeholder="确认密码" />
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleForget">
          提交
        </el-button>
      </el-form-item>
      <el-button type="text" @click="$router.push({ path: '/login' })">
        返回登录
      </el-button>
    </el-form>
  </div>
</template>

<script>
import { isvalidateMobile } from '@/utils/validate'
import { getVerifycode, forgetPassword } from '@/api/user'
export default {
  name: 'Forgetpass',
  data() {
    /* 校验合法手机号 */
    const validateMobile = (rule, value, callback) => {
      if (!isvalidateMobile(value)) {
        callback(new Error('请输入正确手机号码'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (this.forgetForm.confirmPass !== '') {
        this.$refs.forgetForm.validateField('confirmPass')
      }
      callback()
    }
    const validateConfirmpass = (rule, value, callback) => {
      if (this.forgetForm.password !== '' && value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.forgetForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      sendVerifyCode: true,
      btnmsg: '点击获取短信验证码',
      count: 60,
      timer: null,
      forgetForm: {
        mobile: '',
        verifyCode: '',
        password: '',
        confirmPass: ''
      },
      forgetRules: {
        mobile: [{ required: true, message: '请输入手机号', trigger: 'change' }, { validator: validateMobile, trigger: 'change' }],
        verifyCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        password: [{ required: true, message: '请输入新密码', trigger: 'blur' }, { validator: validatePass, trigger: 'blur' }],
        confirmPass: [{ validator: validateConfirmpass, trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    resetVerifyCode() {
      this.sendVerifyCode = true
      this.btnmsg = '点击获取短信验证码'
      this.count = 60
      clearInterval(this.timer)
    },
    handleVerify(form, prop, mobile) {
      this.$refs[form].validateField(prop, (msg) => {
        if (!msg) {
          getVerifycode({ 'mobile': mobile, 'type': 3 }).then(() => {
            this.timer = setInterval(() => {
              console.log(this.timer)
              this.count--
              this.btnmsg = this.count + '秒后重新发送'
              this.sendVerifyCode = false
              if (this.count <= 0) {
                this.resetVerifyCode()
              }
            }, 1000)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleForget() {
      this.$refs.forgetForm.validate(valid => {
        if (valid) {
          this.loading = true
          forgetPassword(this.forgetForm).then(() => {
            this.actiniaSucc('重置密码成功')
            this.$router.push({ path: '/login' })
          }).finally(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg:#2d3a4b;
  $light_gray:#eee;

  /* reset element-ui css */
  .forget-container {
    .el-input {
      height: 40px;
      width: 92%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        //&:-webkit-autofill {
        //  -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        //  -webkit-text-fill-color: #fff !important;
        //}
      }
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;
  .forget-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background: url("../../assets/bg.jpg") no-repeat fixed;
    background-size: cover;
    //background-color: $bg;
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 520px;
      max-width: 100%;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
      background-color: rgba(0,0,0,0.4);
      border-radius: 4px;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
